// This is a tools will covert selected texture to premultiply[1] texture with 'premulti-' prefix.
// [1] http://en.wikipedia.org/wiki/Premultiplied_alpha
// Step 1, copy texture to a temp file and reimport as non-compressed, readable texture.
// Step 2, convert to premultiple texture and import to the same path of original texture, with 'premulti-' prefix.

using System.IO;
using UnityEngine;
using UnityEditor;

public class PremultiplyTextureScript : ScriptableObject {
    [MenuItem ("Tools/PremultiplyTexture")]
    static void PremultiplyTexture () {
        Object[] textures = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
        foreach (Texture2D texture in textures) {
            string path = AssetDatabase.GetAssetPath(texture);
            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            if (!textureImporter.DoesSourceTextureHaveAlpha()) {
                Debug.LogWarning("Can not convert texture without alpha channel.");
                continue;
            }
            AssetDatabase.StartAssetEditing();
            string textureDirName = Path.GetDirectoryName(path);
            string textureName = Path.GetFileName(path);
            string tempTexturePath = Path.Combine(textureDirName, "Temp-" + textureName);
            AssetDatabase.CopyAsset(path, tempTexturePath);
            AssetDatabase.StopAssetEditing();

            TextureImporter tempImporter = AssetImporter.GetAtPath(tempTexturePath) as TextureImporter;
            AssetDatabase.StartAssetEditing();
            tempImporter.isReadable = true;
            tempImporter.textureFormat = TextureImporterFormat.ARGB32;
            AssetDatabase.ImportAsset(tempTexturePath);
            AssetDatabase.StopAssetEditing();

            Texture2D tempTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(tempTexturePath, typeof(Texture2D));
            Texture2D premultiTexture = new Texture2D(tempTexture.width, tempTexture.height, TextureFormat.ARGB32, false);
            Color[] originalPixels = tempTexture.GetPixels();
            Color[] newPixels = new Color[originalPixels.Length];
            for (int i=0; i<originalPixels.Length; i++) {
                newPixels[i].r = originalPixels[i].r * originalPixels[i].a;
                newPixels[i].g = originalPixels[i].g * originalPixels[i].a;
                newPixels[i].b = originalPixels[i].b * originalPixels[i].a;
                newPixels[i].a = originalPixels[i].a;
            }
            premultiTexture.SetPixels(newPixels);
            premultiTexture.Apply();

            string textureNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
            string premultiTexturePath = Path.Combine(textureDirName, "Premulti-" + textureNameWithoutExtension + ".png");
            byte[] bytes = premultiTexture.EncodeToPNG();
            File.WriteAllBytes(premultiTexturePath, bytes);
            DestroyImmediate(premultiTexture);
            AssetDatabase.StartAssetEditing();
            AssetDatabase.DeleteAsset(tempTexturePath);
            AssetDatabase.ImportAsset(premultiTexturePath);
            AssetDatabase.StopAssetEditing();
        }
    }
}
